package org.lyq.erp.process.userrank.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.process.buyfulltemp.pojo.TblWxBuyfullTemplate;
import org.lyq.erp.process.buyfulltemp.service.TblWxBuyfullTemplateService;
import org.lyq.erp.process.userrank.pojo.TblWxUserrank;
import org.lyq.erp.process.userrank.service.TblWxUserrankService;
import org.lyq.erp.utils.datetime.DateUtils;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/TblWxUserrank" })
public class TblWxUserrankController {
	private static Logger logger = LoggerFactory.getLogger(TblWxUserrankController.class);

	@Resource
	private TblWxUserrankService tblWxUserrankService;

	@Resource
	private TblWxBuyfullTemplateService tblWxBuyfullTemplateService;

	@RequestMapping(path = { "/insertAndUpdate" })
	public String insertAndUpdate(TblWxUserrank tblWxUserrank, String menuUuid, ModelMap map) {
		if (!insertValidation(tblWxUserrank)) {
			tblWxUserrank.popuperrorMessage = "true";
			map.put("dataBean", tblWxUserrank);
			getPageDates(map);
			return "process/userrank/tblwxuserrank_list";
		}
		try {
			if (tblWxUserrank.getRankId() != null) {
				this.tblWxUserrankService.updateUserrankByPrimaryKeySelective(tblWxUserrank);
			} else {
				this.tblWxUserrankService.insertUserrankSelective(tblWxUserrank);
			}
		} catch (Exception e) {

			logger.info(e.getMessage());
			tblWxUserrank.addErrorMessage("userrank012");
			tblWxUserrank.popuperrorMessage = "true";
			map.put("dataBean", tblWxUserrank);
			getPageDates(map);
			return "process/userrank/tblwxuserrank_list";
		}
		return "redirect:/TblWxUserrank/list?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/list" })
	public String list(ModelMap map) {
		getPageDates(map);
		return "process/userrank/tblwxuserrank_list";
	}

	@RequestMapping({ "/getTblWxUserrank" })
	@ResponseBody
	public JSONResult getTblWxUserrank(Integer rankId) {
		if (rankId != null) {
			TblWxUserrank tblWxUserrank = this.tblWxUserrankService.selectUserrankByPrimaryKey(rankId);
			if (tblWxUserrank != null) {
				return JSONResult.ok(tblWxUserrank);
			}
			return JSONResult.errorMsg("userrank010");
		}

		return JSONResult.errorMsg("userrank010");
	}

	@RequestMapping({ "/delete" })
	public String delete(TblWxUserrank tblWxUserrank, String menuUuid, ModelMap map) {
		if (tblWxUserrank.getRankId() != null) {
			try {
				this.tblWxUserrankService.deleteUserrankByPrimaryKey(tblWxUserrank.getRankId());
				return "redirect:/TblWxUserrank/list?menuUuid=" + menuUuid;
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}

		tblWxUserrank.addErrorMessage("userrank013");
		map.put("dataBean", tblWxUserrank);
		getPageDates(map);
		return "process/userrank/tblwxuserrank_list";
	}

	private void getPageDates(ModelMap map) {
		List<TblWxUserrank> list = this.tblWxUserrankService.list();
		map.addAttribute("list", list);
		List<TblWxBuyfullTemplate> buyfullTempList = this.tblWxBuyfullTemplateService.selectAllBuyfullTemp();
		map.addAttribute("buyfullTempList", buyfullTempList);
	}

	private boolean insertValidation(TblWxUserrank tblWxUserrank) {
		BigDecimal maxBigMoney = new BigDecimal("9999999999999");

		if (ObjectEmptyUtils.isEmptyString(tblWxUserrank.getRankName())) {
			tblWxUserrank.addErrorMessage("rankName", "userrank001");
		} else if (!ObjectEmptyUtils.isEmptyString(tblWxUserrank.getRankName())
				&& tblWxUserrank.getRankName().trim().length() > 10) {
			tblWxUserrank.addErrorMessage("rankName", "userrank002");
		}

		if (tblWxUserrank.getRankAmtNum() == null || tblWxUserrank.getRankAmtNum().compareTo(BigDecimal.ZERO) == -1
				|| tblWxUserrank.getRankAmtNum().compareTo(BigDecimal.ZERO) == 0
				|| tblWxUserrank.getRankAmtNum().compareTo(maxBigMoney) != -1) {
			tblWxUserrank.addErrorMessage("rankAmtNum", "userrank003");
		}

		if (tblWxUserrank.getRankIstype() == null) {
			tblWxUserrank.addErrorMessage("rankIstype", "userrank004");
		} else if (tblWxUserrank.getRankIstype().intValue() == 1) {
			if (tblWxUserrank.getRankDiscount() == null || tblWxUserrank.getRankDiscount().doubleValue() < 1.0D
					|| tblWxUserrank.getRankDiscount().doubleValue() > 100.0D) {
				tblWxUserrank.addErrorMessage("rankDiscount", "userrank005");
			}
			validationStartTimeAndEnaTime(tblWxUserrank);
		} else if (tblWxUserrank.getRankIstype().intValue() == 2 && tblWxUserrank.getRankBuyfullId() == null) {
			tblWxUserrank.addErrorMessage("rankBuyfullId", "userrank009");
		}

		return tblWxUserrank.getMap().isEmpty();
	}

	private void validationStartTimeAndEnaTime(TblWxUserrank tblWxUserrank) {
		if (ObjectEmptyUtils.isEmptyString(tblWxUserrank.getRankDiscntStatimeView())
				|| ObjectEmptyUtils.isEmptyString(tblWxUserrank.getRankDiscntEndtimeView())
				|| tblWxUserrank.getRankDiscntStatimeView().trim().length() != 10
				|| tblWxUserrank.getRankDiscntEndtimeView().trim().length() != 10) {
			tblWxUserrank.addErrorMessage("rankDiscntStatimeView", "userrank006");
		} else if (!DateUtils.isThanToDay(tblWxUserrank.getRankDiscntStatimeView().trim())) {
			tblWxUserrank.addErrorMessage("rankDiscntStatimeView", "userrank007");
		} else if (!DateUtils.endTimeMoreStartTime(tblWxUserrank.getRankDiscntStatimeView().trim(),
				tblWxUserrank.getRankDiscntEndtimeView().trim())) {
			tblWxUserrank.addErrorMessage("rankDiscntStatimeView", "userrank008");
		}
	}
}
